package com.kernel.web.context;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/****************************************
 * @name WebContext
 * @description WebContext主要是用来存储当前线程中的HttpServletRequest和HttpServletResponse，
 * 当别的地方需要使用HttpServletRequest和HttpServletResponse，就可以通过requestHolder和responseHolder获取，
 * 通过WebContext.java这个类 ，我们可以在作为Controller的普通java类中获取当前请求的request、response或者session相关请求类的实例变量，
 * 并且线程间互不干扰的，因为用到了ThreadLocal这个类。
 * @author 黄彬恒
 * @since 2018/7/19
 ***************************************/
public class WebContext {
    public static ThreadLocal<HttpServletRequest> requestHolder = new ThreadLocal<>();
    public static ThreadLocal<HttpServletResponse> responseHolder = new ThreadLocal<>();

    public HttpServletRequest getRequest() {
        return requestHolder.get();
    }

    public HttpSession getSession() {
        return requestHolder.get().getSession();
    }

    public ServletContext getServletContext() {
        return requestHolder.get().getSession().getServletContext();
    }

    public HttpServletResponse getResponse() {
        return responseHolder.get();
    }
}
